package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_user_cash_records;

import cn.com.api.service.ApiT_user_cash_recordsService;

import cn.com.util.*;


public class T_user_cash_recordsBaseController {

	private static Logger logger = Logger.getLogger(T_user_cash_recordsBaseController.class);
	
	@Autowired
	private ApiT_user_cash_recordsService apiT_user_cash_recordsService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_user_cash_records-list")
	public ModelAndView t_user_cash_records_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_cash_records_list.jsp");
		
		List<ApiT_user_cash_records> t_user_cash_recordss = apiT_user_cash_recordsService.findT_user_cash_recordss();
		mv.addObject("t_user_cash_recordss", t_user_cash_recordss);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_user_cash_records-add-pre")
	public ModelAndView t_user_cash_records_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_cash_records_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_user_cash_records t_user_cash_records = apiT_user_cash_recordsService.findT_user_cash_recordsById(Integer.valueOf(id));
			mv.addObject("t_user_cash_records",t_user_cash_records);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_user_cash_records-add")
	public void t_user_cash_records_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String user_id = request.getParameter("user_id");
		String money = request.getParameter("money");
		String type = request.getParameter("type");
		String remark = request.getParameter("remark");
		String create_time = request.getParameter("create_time");
		String record_id = request.getParameter("record_id");
		String order_id = request.getParameter("order_id");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(user_id==null || (user_id.trim()).equals("")){
			ApiT_user_cash_records t_user_cash_records = new ApiT_user_cash_records();
            /*
		    t_user_cash_records.setUser_id(user_id);
		    t_user_cash_records.setMoney(money);
		    t_user_cash_records.setType(type);
		    t_user_cash_records.setRemark(remark);
		    t_user_cash_records.setCreate_time(create_time);
		    t_user_cash_records.setRecord_id(record_id);
		    t_user_cash_records.setOrder_id(order_id);
	        */
			apiT_user_cash_recordsService.insertT_user_cash_records(t_user_cash_records);
		}
		//edit
		else{
			ApiT_user_cash_records t_user_cash_records = apiT_user_cash_recordsService.findT_user_cash_recordsById(Integer.valueOf(user_id));
	        /*
		    t_user_cash_records.setUser_id(user_id);
		    t_user_cash_records.setMoney(money);
		    t_user_cash_records.setType(type);
		    t_user_cash_records.setRemark(remark);
		    t_user_cash_records.setCreate_time(create_time);
		    t_user_cash_records.setRecord_id(record_id);
		    t_user_cash_records.setOrder_id(order_id);
			*/
			apiT_user_cash_recordsService.updateT_user_cash_records(t_user_cash_records);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_user_cash_records-delete")
	public void t_user_cash_records_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_user_cash_recordsService.deleteT_user_cash_records(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



